/**
 * @todo   抢购时间定时插件
 * @date   20150927
 * @author wnewstar
 */
(function($) {
	$.fn.countdown = function(options) {
		var settings = {'date': null};
		if(options) {
			$.extend(settings, options);
		}
		var timer = setInterval(countdown_proc, 1000);
		var date = new Date($('#show_buy_wrap').data('timestamp') * 1000);
		var obj = $(this);
		function countdown_proc() {
			eventDate = Math.floor(Date.parse(settings['date']) / 1000);
			currentDate = Math.floor($.now() / 1000);
			if(eventDate <= currentDate) {
				clearInterval(timer);
				window.location.reload();
			} else {
				// 时间差秒数
				second_diff = eventDate - currentDate;
			}

			// 倒计时小时
			hours = Math.floor(second_diff / (60 * 60));
			// 倒计时分钟
			minutes = Math.floor((second_diff - hours * 60 * 60) / 60);
			// 倒计时秒数
			seconds = Math.floor(second_diff - hours * 60 * 60 - minutes * 60);

			// 定时显示数字替换
			$('span.timer_num:eq(0)', obj).text(Math.floor(hours / 10));
			$('span.timer_num:eq(1)', obj).text(hours % 10);
			$('span.timer_num:eq(2)', obj).text(Math.floor(minutes / 10));
			$('span.timer_num:eq(3)', obj).text(minutes % 10);
			$('span.timer_num:eq(4)', obj).text(Math.floor(seconds / 10));
			$('span.timer_num:eq(5)', obj).text(seconds % 10);

			var hm = date.getHours() * 100 + date.getMinutes();

			// 文案按时间轮换
			if(hm >= 900 && hm < 1100) {
				$('#word_item_a').show();
			} else if(hm >= 1100 && hm < 1300) {
				$('#word_item_b').show();
			} else if(hm >= 1300 && hm < 1500) {
				$('#word_item_c').show();
			} else if(hm >= 1500 && hm < 1800) {
				$('#word_item_d').show();
			} else if(hm >= 1800 && hm < 2230) {
				$('#word_item_e').show();
			} else if(hm >= 2230 && hm < 2330) {
				$('#word_item_f').show();
			} else {
				$('#word_item_g').show();
			}
			// 获取鸡蛋白剩余数量
			if(second_diff % 5 == 0 && obj.attr('id') == 'timer_1') {
				$.ajax({
					url: 'glair.php?act=remain',
					type: 'post',
					dataType: 'json',
					success: function(data) {
						if(data.error_code == 0) {
							$('#sy_num').text(data.sy_num);
							$('#loss_money').text(data.loss_money);
						}
					}
				});
			}

			// 刷新产量
			if(obj.attr('id') == 'timer_2') {
				if(hm >= 900 && hm < 1100) {
					$('#now_num').text('熬制桃胶：' + Math.floor(135 * 360 - second_diff) + 'ml');
				} else if(hm >= 1100 && hm < 1300) {
					$('#now_num').text('材料融合：' + parseFloat(Math.floor(115 * 360 - second_diff) * 77.8).toFixed(2) + 'ml');
				} else if(hm >= 1300 && hm < 1500) {
					$('#now_num').text('打球研磨');
				} else if(hm >= 1500 && hm < 1800) {
					console.log(Math.floor(75 * 360 - second_diff));
					$('#now_num').text('装瓶：' + parseFloat(Math.floor(75 * 360 - second_diff) * 56 / 108).toFixed(2) + '瓶');
				} else if(hm >= 1800 && hm < 2230) {
					$('#now_num').text('今日产量：5600瓶');
				} else {
					$('#now_num').text('材料准备中');
				}
			}
		}
	}
}) (jQuery);